<template>
  <el-dialog
    :visible="visible"
    :title="isUpdate ? '编辑' : '新增'"
    @close="visible = false"
  >
    <el-form class="form">
      <el-form-item label="产品名称">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="数据来源">
        <el-select
          v-model="formData.flag"
          placeholder="选择数据来源"
        >
          <el-option
            v-for="item in dataOriginOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          v-model="formData.time"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="价格">
        <el-input-number v-model="formData.price" />
      </el-form-item>
      <el-form-item label="单位">
        <el-input-number v-model="formData.unit" />
      </el-form-item>
      <el-form-item label="产地">
        <el-input v-model="formData.originPlace" />
      </el-form-item>
      <el-form-item label="市场">
        <el-input v-model="formData.market" />
      </el-form-item>
      <el-form-item label="规格">
        <el-input v-model="formData.specification" />
      </el-form-item>
      <el-form-item label="报价机构">
        <el-input v-model="formData.quotationAgency" />
      </el-form-item>
    </el-form>
    <el-button @click="visible = false">取 消</el-button>
    <el-button
      type="primary"
      @click="confirm"
    >确 定</el-button>
  </el-dialog>
</template>

<script>
import { getProductPriceById, saveProductPrice, updateProductPrice } from '@/api/bigdata/productPrice'
import { dataOriginList } from '@/utils/dict'
export default {
  data() {
    return {
      visible: false,
      isUpdate: false,
      formData: {
        id: '',
        time: '',
        name: '',
        originPlace: '',
        price: '',
        unit: '',
        market: '',
        specification: '',
        quotationAgency: ''
      },
      dataOriginOptions: dataOriginList
    }
  },
  methods: {
    async init(id) {
      this.visible = true
      if (id != null) {
        this.isUpdate = true
        const data = await getProductPriceById(id)
        this.formData = data.data
      } else {
        this.isUpdate = false
        this.formData = {
          id: '',
          time: '',
          name: '',
          originPlace: '',
          price: '',
          unit: '',
          market: '',
          specification: '',
          quotationAgency: ''
        }
      }
    },
    confirm() {
      if (this.isUpdate) {
        this.handleConform(updateProductPrice, this.formData)
      } else {
        this.handleConform(saveProductPrice, this.formData)
      }
    },
    handleConform(operation) {
      operation(this.formData).then(response => {
        if (response.statusCode == 20000) {
          this.refresh()
          this.visible = false
          this.$message({
            message: '操作成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，操作失败',
            type: 'warning'
          })
        }
      })
    },
    refresh() {
      this.$emit('refresh')
    }
  }

}
</script>

<style>
.form {
  margin-bottom: 20px;
}
</style>
